import {useCallback, useContext, useEffect, useRef, useState} from "react";
import {
	AsyncPageSelect, AsyncPageSelectSearchParam,
	ClaimInput,
	getAllTypesOfMetasByPageCode,
	Selector, ValidateStyleAdjustment,
} from "@/biz-components/claim-items/components";
import {Button, Table} from 'antd';
import { formatMessage } from '@/utils/i18n-transform';
import * as CommonDataSource from '../../../common-data-source';
import {DictionarySelector} from "@/components/dictionary";
import {DetailContext} from "@/biz-components/claim-items/components/DetailContext";
import {Field} from "@formily/core/lib/models";
import {useField} from "@formily/react";
import {minus, plus} from "@ey-utils/utils/numberPrecision";
import ClaimItemsDataUtil from "@/biz-components/claim-items/ClaimItemsDataUtil";

/**
 * @author DongPing
 * 2022/1/12 17:48
 *
 * 收款池-财务认领
 *
 * */

const PAGE_CODE = 'ClaimInfoA_Finance_IT_L';

/**
 * 模型里面配置的必填项字段，组件渲染的时候查询元模型配置，保存必填字段的key，校验的时候用
 * */
let metaModeRequiredKeys: string[] = [];

export function CollectionFinance(props: {
	value: any[],
	onChange: (value: any[]) => void,

	// 认领门店，用于查询下拉选择项
	shop: string,

	// 收款类型，用来控制表格列的显示隐藏
	collectionType: string,
	validateError: boolean
}) {
	const {value = [], onChange, collectionType, validateError} = props;
	const [tableColumnMetas, setTableColumnMetas] = useState<any[]>([]);
	useEffect(function () {
		getAllTypesOfMetasByPageCode(PAGE_CODE).then((result) => {
			setTableColumnMetas(result.tableColumnMetas);
		});
	}, []);
	// 收款明细事项下拉选项查询
	const collectionDetailOptions = CommonDataSource.dictionary.useDictionaryChildrenOptions('BANK_PYMT_FINC_TYPE', props.collectionType)

	useEffect(function () {
		let keys: string[] = [];
		tableColumnMetas.forEach((item) => {
			if(item.props.required === '1') {
				keys.push(item.key)
			}
		});
		metaModeRequiredKeys = keys;
		return function () {
			metaModeRequiredKeys = [];
		}
	}, [tableColumnMetas]);


	/**
	 *http://jira.ecsphere.net:9080/browse/XYL-1421
	 * 保险赔付到账，收款明细事项是工伤工资、伤残补助金、生育保险时，赔付对象只要“员工”项。
	 * 赔付对象就给个默认值员工，指出来的类型不可编辑
	 *
	 * */
	const onlyEmployeeChooseTypes = ['BXPF02', 'BXPF03', 'BXPF05'];

	function onClickAdd() {
		props.onChange(value.concat([{
			thisClaimAmt: 0,
			cllctDetail: collectionDetailOptions.length > 0 ? collectionDetailOptions[0].value : ''
		}]))
	}

	function onDelete(record: any) {
		props.onChange(value.filter((item) => item!==record));
	}
	const getLabel = useCallback((key: string) => {
		return tableColumnMetas.find((item) => item.key === key)?.label;
	}, [tableColumnMetas]);
	const isRequiredFieldInvalid = useCallback((key: string, v: any) => {

		let required = tableColumnMetas.find((item) => item.key === key)?.props.required === '1';
		if(required && validateError) {
			return typeof v === "number" ? false : !v;

		} else {
			return false;
		}
	}, [tableColumnMetas, validateError]);

	// 柜组编码 下拉选择项
	// const querySaleBarCallback = useCallback((p: AsyncPageSelectSearchParam) => {
	// 	return CommonDataSource.shop.queryShopSaleBarsPage(p, props.shop)
	// }, [props.shop]);


	const taxTypeOptions = CommonDataSource.dictionary.useDictionaryOptions('TAX_TYPE');

	// 返还金额与扣款金额是否一致数据字典查询
	//const returnAmountSameOptions = CommonDataSource.dictionary.useDictionaryOptions('YES_OR_NO');

	// 柜组经办人下拉数据
	const saleBarManagerOptionsSearch = useCallback((p: AsyncPageSelectSearchParam) => {
		return CommonDataSource.shop.queryShopPersonsPage(p, props.shop);
	}, [props.shop]);

	const detailMode = useContext(DetailContext);
	const field = useField<Field>();

	let gui_zu_jin_ban_required = collectionType === 'KHYF';

	// 医药费、工伤工资、伤残补助金、生育金时，柜组不能输入，赔付对象必输，选择员工时，员工必输（选择），选择顾客时，顾客必输，输名字；柜组不能输入，门店必输（门店放到认领说明后面）
	let yi_yao_gong_shang_dd_types = ['BXPF01','BXPF02', 'BXPF03', 'BXPF05'];

	useEffect(function () {
		field.setValidator(() => {

			let valid = (value || []).every((item) => {
				for(const requiredKey of metaModeRequiredKeys) {
					if((collectionType === "GJJN") && (requiredKey === 'thisClaimAmt')) continue;
					if(typeof item[requiredKey] === 'number') continue;
					if(!item[requiredKey]) return false;
				}
				if(gui_zu_jin_ban_required && !item.deptMgr) {
					return false;
				}

				/*if(((collectionType === 'YHHD')) && !item.supplierCode) {
					return false;
				}*/
				// if(collectionType === "YHSX" && !item.consistent) {
				// 	return false;
				// }

				if(collectionType === "TS" && !item.taxType) {
					return false;
				}

				if(collectionType === 'KMD' && !item.otherDeptCode) {
					return false;
				}
				if(collectionType === 'KMD' && !item.branchDeptCode) {
					return false;
				}
				if(collectionType === 'KMD' && !item.rebateType) {
					return false;
				}
				//存款、提货券，购物卡到账
				// 提货券到账时，提货卷优惠金额必填，其他不能输入
				// XYL-1539
				if((['CK'].includes(collectionType)) && (item.cllctDetail === 'CK02') && !item.discountAmt) {
					return false;
				}

				if(collectionType === 'GC' && (!item.projectAssetCode || !item.wbs)) {
					return false;
				}
				//保险赔付到账 收款明细事项为：财产赔偿款，柜组必须输入，门店必输（门店放到认领说明后面）
				if(['BXPF', 'BXPF_R'].includes(collectionType) && item.cllctDetail === 'BXPF04' && !item.branchDeptCode) {
					return false;
				}
				if(['BXPF', 'BXPF_R'].includes(collectionType) && item.cllctDetail === 'BXPF04' && (!['BXPF03', 'BXPF05'].includes(item.cllctDetail)) && !item.otherDeptCode) {
					return false;
				}
				if(['BXPF', 'BXPF_R'].includes(collectionType) && yi_yao_gong_shang_dd_types.includes(item.cllctDetail)) {
					if(!onlyEmployeeChooseTypes.includes(item.cllctDetail) && !item.cmpnstnObj) return false;
					// 赔付对象员工
					if(item.cmpnstnObj === 'CO01' && !item.employeeId) {
						return false;
					}
					// 赔付对象顾客
					if(item.cmpnstnObj === 'CO02' && !item.customer) {
						return false;
					}
				}
				// 交利润 柜组必填
				if(collectionType === 'JLR' && !item.branchDeptCode) {
					return false;
				}


				const thisC = Number(item.thisClaimAmt);
				if(isNaN(thisC)) {
					return false;
				} else {
					return thisC > 0;
				}
			});
			if(valid) {
				return null
			} else {
				return {type: 'error', message: '认领明细填写有误'}
			}
		})
	}, [collectionType, value, gui_zu_jin_ban_required]);


	const queryWBSCallback = useCallback(() => {
		// return CommonDataSource.shop.queryProjectWBS(props.shop);
	}, [props.shop]);

	const shopChangeFlagRef = useRef(0);

	return (
		<div className={'finance-claim-detail-items'}>
			{
				detailMode ? null :
					<Button type={"primary"} onClick={onClickAdd}>{formatMessage({id: 'common.add'})}</Button>
			}
			<Table scroll={{x: 'max-content'}} size={"small"} pagination={false} dataSource={value as unknown as object[]}>
				<Table.Column fixed title={formatMessage({ id: 'config.common.label.seq' })} render={(t, r, index) => index + 1}/>
				{
					detailMode ? null :
						<Table.Column fixed title={formatMessage({ id: 'config.common.label.operation' })} render={(t, r) => {
							return(
								<Button size={"small"} onClick={() => onDelete(r)}>{formatMessage({id: 'common.delete'})}</Button>
							)
						}}/>
				}
				<Table.Column dataIndex={'cllctDetail'} title={getLabel('cllctDetail')} render={(t, r: any) => {
					return (
						<ValidateStyleAdjustment invalid={isRequiredFieldInvalid('cllctDetail', r.cllctDetail)}>

						<Selector
							style={{minWidth: '220px'}}
							value={t}
							onChange={(v) => {
								ClaimItemsDataUtil.resetObjectAttributesToEmptyString(r, ['cllctDetail', 'discountAmt']);

								// 保险赔付对象是员工
								if(onlyEmployeeChooseTypes.includes(v)) {
									r.cmpnstnObj = 'CO01';
								}

								r.cllctDetail = v;
								if(v !== 'CK02') {
									r.discountAmt = '';
								}
								r.varAmt = ''
								onChange([...value])
							}}
							multiple={false} dataSource={collectionDetailOptions}/>
						</ValidateStyleAdjustment>
					)
				}}/>
				<Table.Column dataIndex={'thisClaimAmt'} title={getLabel('thisClaimAmt')} render={(t, r: any) => {
					{/*// XYL-1063【来款财务认领】收款类型：股金缴纳（GJJN）{明细的认领金额不可编辑：认领金额=认领股金金额+认领股金利息金额}*/}
					return (
						<ValidateStyleAdjustment invalid={collectionType === 'GJJN' ? false : !(Number(r.thisClaimAmt) > 0)}>
							<ClaimInput
								disabled={collectionType === 'GJJN'}
								type={'number'} value={r.thisClaimAmt} onChange={(e) => {
								if(/^-/.test(e.target.value)) return;
								r.thisClaimAmt = e.target.value;
								onChange([...value])
							}}/>
						</ValidateStyleAdjustment>
					)
				}}/>

				{
					(['BXPF','BXPF_R', 'KMD'].includes(collectionType)) &&
					<>
						<Table.Column dataIndex={'otherDeptCode'} title={getLabel('otherDeptCode')} render={(t, r: any) => {
							return (
								<ValidateStyleAdjustment invalid={
									(!['BXPF03', 'BXPF05'].includes(r.cllctDetail)) &&
									(
										(collectionType === 'KMD' && !t)
										||
										((['BXPF', 'BXPF_R'].includes(collectionType)) && (r.cllctDetail === 'BXPF04') && !t)
										||
										(['BXPF', 'BXPF_r'].includes(collectionType) && yi_yao_gong_shang_dd_types.includes(r.cllctDetail) && !t)
									)
								}>
									<AsyncPageSelect
										disabled={['BXPF03', 'BXPF05'].includes(r.cllctDetail)}
										value={r.otherDeptCode}
										onChange={(v, name) => {
											r.otherDeptCode = v;
											r.otherDeptName = name;

											//XYL-1306 财务认领保险赔付到账和跨门店返利到账，选择相应的门店代码后，后面的柜组代码和员工代码应限制是属于这个门店的
											if(['BXPF','BXPF_R','JLR'].includes(collectionType)) {
												r.branchDeptCode = '';
												r.branchDeptName = '';
												r.employeeId = '';
												r.employeeName = '';
												shopChangeFlagRef.current = shopChangeFlagRef.current === 10000 ? 1 : (shopChangeFlagRef.current + 1)
											}
											onChange([...value])
										}}

									/>
								</ValidateStyleAdjustment>
							)
						}}/>
						<Table.Column dataIndex={'otherDeptName'} title={getLabel('otherDeptName')}/>
					</>
				}
				{
					collectionType === 'GJJN'
					&&
						<>
							<Table.Column dataIndex={'claimShareAmt'} title={getLabel('claimShareAmt')} render={(t, r: any) => {
								return (
									<ValidateStyleAdjustment invalid={isRequiredFieldInvalid('claimShareAmt', r.claimShareAmt)}>
									<ClaimInput type={"number"} value={r.claimShareAmt} onChange={(e) => {
										r.claimShareAmt = e.target.value;
										let na = Number(r.claimShareAmt);
										let nb = Number(r.claimShareIntAmt);
										if(isNaN(na)) na = 0;
										if(isNaN(nb)) nb = 0;
										r.thisClaimAmt = plus(na, nb);
										onChange([...value])
									}}/>
									</ValidateStyleAdjustment>
								)
							}}/>
							<Table.Column dataIndex={'claimShareIntAmt'} title={getLabel('claimShareIntAmt')} render={(t, r: any) => {
								return (
									<ValidateStyleAdjustment invalid={isRequiredFieldInvalid('claimShareIntAmt', r.claimShareIntAmt)}>
										<ClaimInput type={"number"} value={r.claimShareIntAmt} onChange={(e) => {
											r.claimShareIntAmt = e.target.value;
											let na = Number(r.claimShareAmt);
											let nb = Number(r.claimShareIntAmt);
											if(isNaN(na)) na = 0;
											if(isNaN(nb)) nb = 0;
											r.thisClaimAmt = plus(na, nb);
											onChange([...value])
										}}/>
									</ValidateStyleAdjustment>
								)
							}}/>
						</>
				}

				{
					['CK'].includes(collectionType) &&
					<Table.Column dataIndex={'discountAmt'} title={getLabel('discountAmt')} render={(t, r: any) => {
						return (
							<ValidateStyleAdjustment invalid={['CK'].includes(collectionType) && r.cllctDetail === 'CK02' && !t}>
								<ClaimInput
									disabled={r.cllctDetail !== 'CK02'}
									type={"number"} value={r.discountAmt} onChange={(e) => {
									r.discountAmt = e.target.value;
									onChange([...value])
								}}/>
							</ValidateStyleAdjustment>
						)
					}}/>
				}

				{
					collectionType === 'TS' &&
					<Table.Column dataIndex={'taxType'} title={getLabel('taxType')} render={(t, r: any) => {
						return (
							<ValidateStyleAdjustment invalid={collectionType === 'TS' && !t}>
								<Selector<string> multiple={false} value={r.taxType} onChange={(v) => {
									r.taxType = v;
									onChange([...value])
								}} dataSource={taxTypeOptions}/>
							</ValidateStyleAdjustment>
						)
					}}/>
				}

				{/*{*/}
				{/*	collectionType === 'YHSX' &&*/}
				{/*	<Table.Column dataIndex={'consistent'} title={getLabel('consistent')} render={(t, r: any) => {*/}
				{/*		return (*/}
				{/*			<ValidateStyleAdjustment invalid={(collectionType === "YHSX") && !t}>*/}
				{/*				<Selector<string> multiple={false} value={r.consistent} onChange={(v) => {*/}
				{/*					r.consistent = v;*/}
				{/*					onChange([...value])*/}
				{/*				}} dataSource={returnAmountSameOptions}/>*/}
				{/*			</ValidateStyleAdjustment>*/}
				{/*		)*/}
				{/*	}}/>*/}
				{/*}*/}

				{
					collectionType === 'KHYF' &&
					<Table.Column dataIndex={'deptMgr'} title={getLabel('deptMgr')} render={(t, r: any) => {
						return (
							<ValidateStyleAdjustment invalid={gui_zu_jin_ban_required && !t}>
								<AsyncPageSelect value={r.deptMgr} onChange={(v) => {
									r.deptMgr = v;
									onChange([...value])
								}} search={saleBarManagerOptionsSearch}/>
							</ValidateStyleAdjustment>
						)
					}}/>
				}

				{
					((collectionType === 'JLR') || (collectionType === 'BXPF') || (collectionType === 'BXPF_R')) &&
					<>
						<Table.Column dataIndex={'branchDeptCode'} title={getLabel('branchDeptCode')} render={(t, r: any) => {
							return (
								<ValidateStyleAdjustment invalid={(((collectionType === 'BXPF') || (collectionType === 'BXPF_R')) && r.cllctDetail === 'BXPF04' && !t) || (collectionType === 'JLR' && !t)}>
									<AsyncPageSelect
										refreshOptionsFlag={shopChangeFlagRef.current}
										disabled={((collectionType === 'BXPF') || (collectionType === 'BXPF_R')) && yi_yao_gong_shang_dd_types.includes(r.cllctDetail)}
										valueAsSelectResult value={r.branchDeptCode} onChange={(v, name) => {
										r.branchDeptCode = v;
										r.branchDeptName = name
										onChange([...value])
									}} search={(p) => {
										return (['BXPF','BXPF_R','JLR'].includes(collectionType) && r.otherDeptCode) ? CommonDataSource.shop.queryShopSaleBarsPage(p, r.otherDeptCode) : CommonDataSource.shop.queryShopSaleBarsPage(p, props.shop);
									}}/>
								</ValidateStyleAdjustment>
							)
						}}/>
						<Table.Column dataIndex={'branchDeptName'} title={getLabel('branchDeptName')}/>
					</>
				}


				{
					((collectionType === "BXPF") || (collectionType === "BXPF_R")) &&
						<>
							<Table.Column dataIndex={'cmpnstnObj'} title={getLabel('cmpnstnObj')} render={(t, r: any) => {
								return (
									<ValidateStyleAdjustment invalid={yi_yao_gong_shang_dd_types.includes(r.cllctDetail) && !t}>
										<DictionarySelector
											disabled={(r.cllctDetail === 'BXPF04') || onlyEmployeeChooseTypes.includes(r.cllctDetail)}
											value={r.cmpnstnObj} onChange={(v) => {
											r.cmpnstnObj = v;
											r.employeeId = '';
											r.employeeName = '';
											r.customer = '';
											onChange([...value])
										}} dictType={'CMPNSTN_OBJ'}/>
									</ValidateStyleAdjustment>
								)
							}}/>

							<Table.Column dataIndex={'employeeId'} title={getLabel('employeeId')} render={(t, r: any) => {
								return (
									<ValidateStyleAdjustment invalid={yi_yao_gong_shang_dd_types.includes(r.cllctDetail) && (r.cmpnstnObj ==='CO01') && !t}>
										<AsyncPageSelect
											disabled={(yi_yao_gong_shang_dd_types.includes(r.cllctDetail) && (r.cmpnstnObj ==='CO02')) || (r.cllctDetail === 'BXPF04')}
											valueAsSelectResult value={r.employeeId} onChange={(v, name) => {
											// 选择员工之后，设置员工姓名
											r.employeeId = v;
											r.employeeName = name;
											onChange([...value])
										}} search={(p) => {
											let shopCode: string | null;
											let otherDeptCode: string | null;
											/**
											 * http://jira.ecsphere.net:9080/browse/XYL-1671
											 * 、改成当选择生育金和伤残补助金的时候，员工代码不受认领门店的控制，可以选到所有门店的人员代码
											 * 当选择医药费、工伤工资不受前面门店代码的控制，可以选到所有门店的人员代码
											 * */
											if(['BXPF05', 'BXPF03', 'BXPF01', 'BXPF02'].includes(r.cllctDetail)) {
												shopCode = null;
												otherDeptCode = null
											} else {
												shopCode = props.shop;
												otherDeptCode = r.otherDeptCode;
											}

											return (['BXPF','BXPF_R','JLR'].includes(collectionType)  && ['BXPF01', 'BXPF02'].includes(r.cllctDetail))? CommonDataSource.shop.queryShopEmployees(p, otherDeptCode  ) : CommonDataSource.shop.queryShopEmployees(p, shopCode);
										}}/>
									</ValidateStyleAdjustment>
								)
							}}/>
							<Table.Column dataIndex={'employeeName'} title={getLabel('employeeName')}/>

							<Table.Column dataIndex={'customer'} title={getLabel('customer')} render={(t, r: any) => {
								return (
									<ValidateStyleAdjustment invalid={
										!onlyEmployeeChooseTypes.includes(r.cllctDetail)
										&&
										(
											yi_yao_gong_shang_dd_types.includes(r.cllctDetail)
											&&
											r.cmpnstnObj ==='CO02'
											&& !t
										)
									}>
										<ClaimInput
											disabled={
												(yi_yao_gong_shang_dd_types.includes(r.cllctDetail) && (r.cmpnstnObj ==='CO01'))
												||
												(r.cllctDetail === 'BXPF04')
												||
												(onlyEmployeeChooseTypes.includes(r.cllctDetail))
											}
											value={r.customer} onChange={(e) => {
											r.customer = e.target.value;
											onChange([...value])
										}} />
									</ValidateStyleAdjustment>
								)
							}}/>
						</>
				}



				{
					['KMD'].includes(collectionType) &&
						<>

							<Table.Column dataIndex={'branchDeptCode'} title={getLabel('branchDeptCode')} render={(t, r: any) => {
								return (
									<ValidateStyleAdjustment invalid={collectionType === 'KMD' && !t}>
										<AsyncPageSelect
											valueAsSelectResult
											value={r.branchDeptCode} onChange={(v, name) => {
											r.branchDeptCode = v;
											r.branchDeptName = name;
											onChange([...value]);
										}} search={(p) => CommonDataSource.shop.queryShopSaleBarsPage(p, r.otherDeptCode)}/>
									</ValidateStyleAdjustment>
								)
							}}/>
							<Table.Column dataIndex={'branchDeptName'} title={getLabel('branchDeptName')}/>
						</>
				}

				{
					collectionType === 'KMD' &&
					<Table.Column dataIndex={'rebateType'} title={getLabel('rebateType')} render={(t, r: any) => {
						return (
							<ValidateStyleAdjustment invalid={collectionType === 'KMD' && !t}>
								<DictionarySelector<string> multiple={false} value={r.rebateType} onChange={(v) => {
									r.rebateType = v;
									onChange([...value])
								}} dictType={"REBATE_TYPE"}/>
							</ValidateStyleAdjustment>
						)
					}}/>
				}

				{
					collectionType === 'GC' &&
						<>
							<Table.Column dataIndex={'projectAssetCode'} title={getLabel('projectAssetCode')} render={(t, r: any) => {
								return (
									<ValidateStyleAdjustment invalid={collectionType === 'GC' && !t}>
										<AsyncPageSelect
											valueAsSelectResult
											value={r.projectAssetCode} onChange={(v, name) => {
											r.projectAssetCode = v;
											r.projectAssetName = name
											onChange([...value])
										}} search={CommonDataSource.shop.queryProjectAssetsPage}/>
									</ValidateStyleAdjustment>
								)
							}}/>
							<Table.Column dataIndex={'projectAssetName'} title={getLabel('projectAssetName')}/>
						</>
				}
				{
					collectionType === 'GC' &&
					<>
						<Table.Column dataIndex={'wbs'} title={getLabel('wbs')} render={(t, r: any) => {
							return (
								<ValidateStyleAdjustment invalid={collectionType === 'GC' && !t}>
									<Selector
										value={r.wbs} onChange={(v) => {
										r.wbs = v;
										onChange([...value])
									}} dataSource={queryWBSCallback}/>
								</ValidateStyleAdjustment>
							)
						}}/>
					</>
				}
				{/*{
					((collectionType === 'YHHD')) &&
						<>
							<Table.Column dataIndex={'supplierCode'} title={getLabel('supplierCode')} render={(t, r: any) => {
								return (
									<ValidateStyleAdjustment invalid={((collectionType === 'YHHD') ) && !t}>
										<AsyncPageSelect
											valueAsSelectResult
											value={r.supplierCode} onChange={(v, name) => {
											r.supplierCode = v;
											r.supplierName = name
											onChange([...value])
										}} search={(p) => {
											return CommonDataSource.shop.queryShopSuppliersPageAll(p, props.shop)
										}}/>
									</ValidateStyleAdjustment>
								)
							}}/>
							<Table.Column dataIndex={'supplierName'} title={getLabel('supplierName')}/>
						</>
				}*/}
				{
					((collectionType === 'BXPF') || (collectionType === 'BXPF'))  &&
					<>
						<Table.Column dataIndex={'varAmt'} title={getLabel('varAmt')} render={(t, r: any) => {
							return (
								<ValidateStyleAdjustment invalid={false}>
									<ClaimInput
										type={"number"}
										disabled={r.cllctDetail !== 'BXPF01'}
										value={r.varAmt} onChange={(e) => {
										r.varAmt = Number(e.target.value);
										onChange([...value])
									}} />
								</ValidateStyleAdjustment>
							)
						}}/>
					</>
				}
				<Table.Column dataIndex={'claimDescr'} title={getLabel('claimDescr')} render={(t, r: any) => {
					return (
						<ValidateStyleAdjustment invalid={isRequiredFieldInvalid('claimDescr', r.claimDescr)}>
							<ClaimInput value={r.claimDescr} onChange={(e) => {
								r.claimDescr = e.target.value;
								onChange([...value])
							}}/>
						</ValidateStyleAdjustment>
					)
				}}/>
			</Table>
		</div>
	)
}
CollectionFinance.calcTotalAmount = calcTotalAmount;
CollectionFinance.haveSpecialCollectionDetailType = haveSpecialCollectionDetailType;


/**
 * 计算明细汇总
 * @param data
 * @param {string} collectType 收款类型
 * */
function calcTotalAmount(data: any[], collectType: string = ''): number {
	let total = 0;

	// XYL-1063【来款财务认领】收款类型：股金缴纳（GJJN）{明细的认领金额不可编辑：认领金额=认领股金金额+认领股金利息金额}
	if(collectType === "GJJN") {
		for(let item of data) {
			let n1 = Number(item.claimShareAmt);
			let n2 = Number(item.claimShareIntAmt)
			if(!isNaN(n1)) {
				total = plus(total, n1);
			}
			if(!isNaN(n2)) {
				total = plus(total, n2);
			}
		}
	} else {
		for(let item of data) {
			let n = Number(item.thisClaimAmt);
			if(!isNaN(n)) {
				total = plus(total, n);
			}
			//XYL-1275 认领总金额应该等于认领金额减去提货券优惠金额
			if(item.cllctDetail === 'CK02') {
				let th_yh = Number(item.discountAmt);
				if(!isNaN(th_yh)) {
					total = minus(total, th_yh);
				}
			}



		}
	}

	return total;
}

/**
 * http://jira.ecsphere.net:9080/browse/XYL-1304
 * 收款中是否包含伤残补助金或生育保险
 * 1、当认领明细的收款明细事项选择截图的两种时，“是否立即付款“字段显示

2、如果存在一条明细存在一个收款明细事项不是截图的两种时，此字段否则隐藏
 * */
function haveSpecialCollectionDetailType(data: any[] = []): boolean {
	return data?.every((item) => {
		return ['BXPF03', 'BXPF05'].includes(item.cllctDetail);
	}) && (data.length > 0);

}

/**
 * 是否包含京东到家返款
 * http://jira.ecsphere.net:9080/browse/XYL-1262
 * */
CollectionFinance.haveJD = function (data: any[] = []): boolean {
	return (data.length > 0) && data.some((item) => {
		return item.cllctDetail === 'JLR01'
	})
}
